package com.dataaccess.dataaccess.support.ffmpeg;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Author: YangYu
 * @ClassName: VideoUtil
 * @Date: Created in 16:02 2020/11/17
 * @Description:
 */
public class VideoUtil {

        public static void main(String[] args) {
            try {
                //E:\WisdIot物联网  E:\WisdIot物联网\ffmpeg\ffmpeg-N-99925-g96443ab4b0-win64-gpl-shared\bin
                getImgFromVideo("E:\\WisdIot物联网\\ffmpeg\\ffmpeg-N-99925-g96443ab4b0-win64-gpl-shared\\bin\\ffmpeg.exe", "E:\\WisdIot物联网\\1.mp4", "E:\\WisdIot物联网\\4.mp4", 204, 140, 0, 0, 26);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**
         * 获取指定时间内的图片
         * @param ffmpegPath (ffmpeg路径)
         * @param videoPath (视频路径)
         * @param imgPath (图片存放路径)
         * @param width (图片宽度)
         * @param height (图片高度)
         * 以下为需要指定的时间
         * @param hour
         * @param min
         * @param sec
         * @return
         */
        public static boolean getImgFromVideo(String ffmpegPath, String videoPath, String imgPath, int width,
                                              int height, int hour, int min, float sec) {
            try {
//                ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-y",
//                        "-i", videoPath, "-vframes", "1", "-ss", hour + ":" + min
//                        + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
//                        "-an", imgPath);
                ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath,
                        "-i", videoPath, "-ss", "00:00:00", "-to", "00:00:10",  imgPath , "-y");
                processBuilder.command().forEach((k)->{
                    System.out.println(k);
                });

                Process process = processBuilder.start();
                InputStream stderr = process.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                while ((br.readLine()) != null);
                process.waitFor();
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (stderr != null)
                    stderr.close();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

    }
